package com.ykdl.member.views;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.ykdl.member.kid.R;
import com.ykdl.member.KidApp;
/**
 * 
 * 巧妈记账的饼状图
 *
 * @since 1.0
 */
public class DrawArcView extends View implements Runnable {
	public static final String TAG = "DrawArcView";

	public static final long SHEEL_TIME = 300;
	public static final int SMALLSCREEN_ARC_FONTZIZE = 16;
	public static final int NORMALSCREEN_ARC_FONTZIZE = 24;
	private int arcFontSize = 0;
//	private Context mContext;

	private float mWidth;
	private float mHeight;
	private float mArcspace = 200;

	private float mDensity;

	private boolean mUpdate;
	private boolean mIsRun;

	private DrawArcData mDrawArcData;
	private DrawArcControl mDrawArcControl;

	private boolean isBig = false;

	// public DrawArcView(Context context, AttributeSet attrs, int defStyle)
	// {
	// super(context, attrs, defStyle);
	// // TODO Auto-generated constructor stub
	// init(context, attrs, defStyle);
	// }

	public DrawArcView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		mArcspace = getResources().getDimension(R.dimen.ka_arcview_arcsapce);
		init(context, attrs, -1);
	}

	public DrawArcView(Context context, float width, float height) {
		super(context);
		// TODO Auto-generated constructor stub
		mWidth = width;
		mHeight = height;
		mArcspace = getResources().getDimension(
				R.dimen.ka_arcview_montharcsapce);
		init(context, null, -1);
	}

	private void init(Context context, AttributeSet attrs, int defStyle) {
	//	mContext = context;

		mDensity = 1;// ViewUtil.getDensity(mContext);
		if (attrs != null) {
	//		float[] viewSize = ViewUtil.getViewSize(attrs, mDensity);
			Resources r = getResources();
			mWidth = r.getDimension(R.dimen.ka_arcview_wid);
			mHeight = r.getDimension(R.dimen.ka_arcview_hei);
			
			// mArcspace=viewSize[2];
		}else{
			
		}

		mWidth *= mDensity;
		mHeight *= mDensity;

		mDrawArcData = new DrawArcData(mWidth, mHeight, mArcspace);

		mDrawArcControl = new DrawArcControl(this);
		if (KidApp.SCREEN_TYPE == KidApp.SCREEN_SMALL) {
			arcFontSize = SMALLSCREEN_ARC_FONTZIZE;
		} else {
			arcFontSize = NORMALSCREEN_ARC_FONTZIZE;
		}

	}

	public DrawArcData getDrawArcData() {
		return mDrawArcData;
	}

	public DrawArcControl getControl() {
		return mDrawArcControl;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		mDrawArcControl.setTouch(event);
		return true;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);

		// 画背景
//		int id = 0;
//		if (!isBig) {
//			id = mDrawArcData.getBackground_id();
//		} else {
//			id = mDrawArcData.getBackground_id2();
//		}
//		Bitmap background = mDrawArcData.getBitmapById(mContext, id);
//		if (background != null) {
//			canvas.drawBitmap(background, (mWidth - background.getWidth()) / 2,
//					(mHeight - background.getHeight()) / 2,
//					mDrawArcData.getPaint(DrawArcData.PAINT_BACKGROUND));
//		}

		// 画扇形
		drawArc(canvas);

		drawTotal(canvas);

		drawLines(canvas);
	}

	private void drawLines(Canvas canvas) {
		int size = mDrawArcData.getArc_size();
		canvas.translate(mDrawArcData.getmCentralX(), mDrawArcData.getmCentralY());
		String []linetext=mDrawArcData.getLine_text();
		Point[] textpos=mDrawArcData.getText_start();
		for(int i=0;i<size;i++){
			Point[]temp=mDrawArcData.getLinePoint(i);
			if(temp!=null){
				canvas.drawLine(temp[0].x, temp[0].y, temp[1].x, temp[1].y, mDrawArcData.getPaint(DrawArcData.PAINT_BACKGROUND));
				canvas.drawText(linetext[i],textpos[i].x, textpos[i].y, mDrawArcData.getPaint(DrawArcData.PAINT_LINE));
			}
		}
		canvas.translate(-mDrawArcData.getmCentralX(), -mDrawArcData.getmCentralY());
		
	}

	private void drawTotal(Canvas canvas) {
//		Bitmap total = mDrawArcData.getBitmapById(mContext,
//				mDrawArcData.getTotal_id());
//		if (total != null) {
//			// Rect src = mDrawArcData.getTotalRect("src");
//			// Rect dst = mDrawArcData.getTotalRect("dst");
//			float imgx = (mWidth - total.getWidth()) / 2;
//			float imgy = (mHeight - total.getHeight()) / 2;
//			canvas.drawBitmap(total, imgx, imgy,
//					mDrawArcData.getPaint(DrawArcData.PAINT_BACKGROUND));
//		}
			// canvas.drawBitmap(total, src, dst,
			// mDrawArcData.getPaint(DrawArcData.PAINT_BACKGROUND));

			String text;
			float x;
			float y;
			Paint paint;
			text = mDrawArcData.getmDefTotal();
			if (text != null && !"".equals(text)) {
				float offsetX;
				paint = mDrawArcData.getPaint(DrawArcData.PAINT_TOTALTEXT);
				paint.setTextSize(arcFontSize);
				float textLen = paint.measureText(text);
				offsetX = textLen / 2;

				x = mDrawArcData.getmCentralX() - offsetX;
				y = mDrawArcData.getmCentralY() - 20 + 10;
				canvas.drawText(text, x, y, paint);
			}

			text = mDrawArcData.getmNumTotal();
			if (text != null && !"".equals(text)) {
				float offsetX;
				paint = mDrawArcData.getPaint(DrawArcData.PAINT_TOTALTEXT);
				paint.setTextSize(arcFontSize);

				float textLen = paint.measureText(text);
				offsetX = textLen / 2;
				x = mDrawArcData.getmCentralX() - offsetX;
				y = mDrawArcData.getmCentralY() + 20;
				canvas.drawText(text, x, y, paint);
			}
		
	}

	public void setOnClickInterval(DrawArcControl.OnClickInterval clickInterval) {
		mDrawArcControl.setOnClickInterval(clickInterval);
	}

	private void drawArc(Canvas canvas) {
		RectF oval = mDrawArcData.getArc_Rect();
		canvas.drawArc(oval, 0, 360, true,
				mDrawArcData.getPaint(DrawArcData.TALLYTYPE_DEFAULT));
		if (!"￥0.0".equals(mDrawArcData.getmNumTotal())) {
			int size = mDrawArcData.getArc_size();
			for (int i = 0; i < size; i++) {
				float[] arc = mDrawArcData.getArcByIndex(i);
				if (arc[2] != 0) {
					canvas.drawArc(oval, arc[1], arc[2], true,
							mDrawArcData.getPaint((int) arc[0]));
				}
			}
		}
	}

	public void setUpdate() {
		mUpdate = true;
	}

	private void closeThread() {
		mIsRun = false;

	}

	@Override
	protected void finalize() throws Throwable {
		// TODO Auto-generated method stub
		super.finalize();

		closeThread();
	}

	@Override
	public void run() {
		while (mIsRun) {
			if (mUpdate) {
				postInvalidate();
				mUpdate = false;
			} else {
				try {
					Thread.sleep(SHEEL_TIME);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}

	}

	public boolean isBig() {
		return isBig;
	}

	public void setBig(boolean isBig) {
		this.isBig = isBig;
	}

}
